package com.hxy.travel.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.travel.constant.MessageConstant;
import com.hxy.travel.entity.PageResult;
import com.hxy.travel.entity.QueryPageBean;
import com.hxy.travel.entity.Result;
import com.hxy.travel.pojo.TravelItem;
import com.hxy.travel.service.TravelItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/travelitem")
public class TravelItemController {

     @Reference
     private TravelItemService travelItemService;

    @PreAuthorize("hasAuthority('TRAVELITEM_ADD')")
    @PutMapping("/add.do")
    public Result add(@RequestBody TravelItem travelItem){

        try {
            travelItemService.addTravelItem(travelItem);

            return Result.ok(MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(MessageConstant.ADD_TRAVELITEM_FAIL);
        }

    }
    @PreAuthorize("hasAuthority('TRAVELITEM_QUERY')")
    @PostMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        PageResult list = travelItemService.selectTravelItemList(queryPageBean);

        return list;

    }
    @PreAuthorize("hasAuthority('TRAVELGROUP_DELETE')")
    @GetMapping("/delete.do")
    public Result delete(Integer id){

        try {
            travelItemService.deleteTravelItemById(id);
            return Result.ok(MessageConstant.DELETE_TRAVELITEM_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ok(MessageConstant.DELETE_TRAVELITEM_FAIL);
        }

    }
    @GetMapping("/selectById.do")
    public Result selectById(Integer id){

      TravelItem travelItem=travelItemService.selectById(id);

      return Result.data(travelItem);

    }
    @PreAuthorize("hasAuthority('TRAVELITEM_EDIT')")
    @PostMapping("/edit.do")
    public Result edit(@RequestBody TravelItem travelItem){

        travelItemService.editTravelItem(travelItem);


        return Result.ok(MessageConstant.EDIT_TRAVELITEM_SUCCESS);

    }

    @GetMapping("/list.do")
    public Result list(){
        List<TravelItem> list =travelItemService.selectTravelItemList();

        return Result.data(list,MessageConstant.QUERY_TRAVELGROUP_SUCCESS);

    }
}
